<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>学习提醒</span>
      </div>
    </template>
    
    <div class="search-form">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-select
            v-model="formInline.studyPlanType"
            placeholder="请选择提醒类别"
            clearable
          >
            <el-option label="学习提醒" value="学习提醒" />
            <el-option label="上报提醒" value="上报提醒" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formInline.studyUnits" placeholder="请输入通知单位" clearable @keyup.enter="onSubmit"></el-input>
          <!-- <el-date-picker
            v-model="formInline.studyPlanDate"
            type="date"
            placeholder="请选择提醒日期"
            value-format="YYYY-MM-DD"
            clearable
          /> -->
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">
            <el-icon>
              <Search />
            </el-icon>
          </el-button>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="addRemind">新建</el-button>
      </div>
    </div>

    <div class="table-data" v-loading="loading">
      <el-table
        :data="remindData"
        stripe
        borderresizable
        highlight-hover-row
        show-overflow
        keep-source
        scrollbar-always-on
      >
        <el-table-column
          v-for="(tableItem, tableIndex) in remindTableColumns" :key="tableIndex"
          :prop="tableItem.field"
          :label="tableItem.label"
          :min-width="tableItem.minWidth"
          :formatter="tableItem.formatter"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          fixed="right"
          field="opera"
          label="操作"
          min-width="180"
        >
          <template #default="{ row }">
            <el-button
              type="warning"
              link
              @click="remindDetail(row)"
            >查看</el-button>
            <el-button
              type="warning"
              link
              @click="studyPlanDateLook(row)"
            >查看提醒日期</el-button>
            <el-button
              type="success"
              link
              @click="remindEdit(row)"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="table-page">
        <el-pagination background layout="prev, pager, next" :total="dataTotal" :page-size="pageSize" :current-page="currentPage" @current-change="currentChange" />
      </div>
    </div>

    <!-- 提醒日期弹窗 -->
    <el-dialog v-model="dialogTableVisible" title="查看提醒日期">
      <div class="" v-for="(tItem, tIndex) in studyPlanDateList" :key="tIndex">
        {{ tItem.studyPlanDate }}
      </div>
    </el-dialog>

  </el-card>
</template>

<script>
export default { name: "backendLearnRemind"}
</script>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import { http } from '@/utils/http/index';
import { dateFormatter, checkStatusFormatter } from "@/utils/backend.js"

/* 路由 */
const route = useRouter();

onMounted(() => {
  onSubmit()
})

/* 检索表单 */
const formInline = ref({
  studyPlanType: "",
  studyUnits: "",
})

/* 分页 */
const dataTotal = ref(0)
const pageSize = ref(20)
const currentPage = ref(1)

if(route.currentRoute.value.query.currentPage) {
  currentPage.value = Number(route.currentRoute.value.query.currentPage)
}

/* 切换页码方法 */
const currentChange = (val) => {
  currentPage.value = val
  onSubmit()
}

/* 表格数据加载动画 */
const loading = ref(false)

/* 表格数据 */
const remindData = ref([])

/* 表格列字段 */
const remindTableColumns = ref([
  { field: "studyPlanType", label: "提醒类别", minWidth: "160" },
  { field: "studyUnits", label: "通知单位", minWidth: "200" },
  { field: "studySubjectName", label: "学习主题", minWidth: "160" },
  { field: "pubDate", label: "发布日期", minWidth: "170", formatter: dateFormatter },
  /* { field: "studyPlanDate", label: "提醒日期", minWidth: "170" }, */
  { field: "studyBak", label: "备注", minWidth: "160" },
  /* { field: "checkStatus", label: "状态", minWidth: "120", formatter: checkStatusFormatter }, */
])

/* 列表查询 */
const onSubmit = () => {
  loading.value = true
  let params = {
    pageIndex: currentPage.value,
    pageSize: pageSize.value,
  }
  let dfi = {
    Logic: 0,
    Filters: []
  }

  for (let k in formInline.value){
    if(formInline.value[k]){
      dfi.Filters.push({
        field: k,
        operator: 0,
        value: formInline.value[k]
      })
    }
  }

  if(dfi.Filters.length){
    params.dfi = dfi
  }
  http.$POST("/TheoryStudyM/GetTheoryStudyPlanInfoList", params).then(res => {
    loading.value = false
    if (res.isError) return
    remindData.value = res.result.dataLst
    dataTotal.value = res.result.countAll
  }).catch(err => {
    loading.value = false
    ElMessage.warning("查询失败，请重试")
  })
}

/* 点击新建 */
const addRemind = () => {
  route.push({
    path: "/backend/learn-remind/add",
    query: { type: 'add' }
  })
}

/* 点击编辑 */
const remindEdit = (row) => {
  route.push({
    path: "/backend/learn-remind/add",
    query: { id: row.id, type: 'edit', currentPage: currentPage.value }
  })
}

/* 查看详情 */
const remindDetail = (row) => {
  let { href } = route.resolve({
    path: "/backend/learn-remind/detail",
    query: { id: row.id, currentPage: currentPage.value }
  })
  window.open(href, "_blank")
}

/* 学习提醒弹窗显隐 */
const dialogTableVisible = ref(false)

/* 提醒日期数据及查询参数 */
const studyPlanDateList = ref([])

const orderSort = (a, b) => {
  return new Date(a).getTime() - new Date(b).getTime()
}

/* 查看提醒日期 */
const studyPlanDateLook = (row) => {
  dialogTableVisible.value = true

  let params = {
    pageIndex: 1,
    pageSize: 100,
    dfi: {
      Logic: 0,
      Filters: [{
        field: "studyPlanUniqueId",
        operator: 8,
        value: row.id
      }]
    }
  }
  http.$POST("/TheoryStudy/GetTheoryStudyPlanDateList", params).then(res => {
    if(res.isError) return
    let arr = res.result.dataLst
    studyPlanDateList.value = arr.sort(orderSort)
  }).catch(err => {
    ElMessage.warning("查询失败")
  })
}
</script>

<style lang='scss' scoped>
@import "@/assets/styles/backendElement.scss";
</style>